﻿//
//  Monkey Manager (Based on nVLC (by Roman Ginzburg))
//  Copyright (C) 2010 Victor Tereschenko (aka sibvic)
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//     
// ========================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using ManagerDB;

namespace MonkeyManager
{
    /// <summary>
    /// Interaction logic for InfoWindow.xaml
    /// </summary>
    public partial class InfoWindow : Window
    {
        public InfoWindow(StoredFile file, bool allowRenaming, IDatabase database)
        {
            mDatabase = database;
            mAllowRenaming = allowRenaming;
            mFile = file;
            InitializeComponent();
            fillControls();
        }

        IDatabase mDatabase;
        StoredFile mFile;
        bool mAllowRenaming;

        /// <summary>
        /// Fill controls with data.
        /// </summary>
        void fillControls()
        {
            titleTextBox.Text = mFile.Title;
            descriptionText.Text = mFile.Description;
            if (mFile.Year == 0 || mFile.Day == 0 || mFile.Month == 0)
                datePicker.SelectedDate = null;
            else
                datePicker.SelectedDate = new DateTime((int)mFile.Year, (int)mFile.Month, (int)mFile.Day);

            StringBuilder tagsSB = new StringBuilder();
            foreach (string tag in mFile.Tags)
                tagsSB.Append(tag + ", ");

            tagsTextBox.Text = tagsSB.ToString();
            tthTextBox.Text = mFile.File.ID.TTH;
        }

        /// <summary>
        /// Fills data from controls.
        /// </summary>
        void fillData()
        {
            mFile.Title = titleTextBox.Text;
            mFile.Description = descriptionText.Text;
            if (datePicker.SelectedDate != null)
            {
                mFile.Year = datePicker.SelectedDate.Value.Year;
                mFile.Day = datePicker.SelectedDate.Value.Day;
                mFile.Month = datePicker.SelectedDate.Value.Month;
            }
            mFile.File.ID.TTH = tthTextBox.Text;
            mFile.Tags.Clear();
            List<string> tags = new List<string>();
            tags.AddRange(StoredFileController.GetTags(tagsTextBox.Text));
            foreach (string tag in tags)
            {
                string tagStr = tag.Trim();
                if (tagStr.Length == 0)
                    continue;

                mFile.Tags.Add(tagStr);
            }
            StoredFileController.Autotag(mFile);
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            fillData();
            if (mAllowRenaming)
                mDatabase.Update(mFile);
            Close();
        }
    }
}
